<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\User;
use app\common\controller\Backend;
use app\common\model\Attachment;
use fast\Date;
use think\Db;

/**
 * 控制台
 *
 * @icon   fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index2()
    {
        try {
            \think\Db::execute("SET @@sql_mode='';");
        } catch (\Exception $e) {

        }
        $column = [];
        $starttime = Date::unixtime('day', -6);
        $endtime = Date::unixtime('day', 0, 'end');
        $joinlist = Db("user")->where('jointime', 'between time', [$starttime, $endtime])
            ->field('jointime, status, COUNT(*) AS nums, DATE_FORMAT(FROM_UNIXTIME(jointime), "%Y-%m-%d") AS join_date')
            ->group('join_date')
            ->select();
        for ($time = $starttime; $time <= $endtime;) {
            $column[] = date("Y-m-d", $time);
            $time += 86400;
        }
        $userlist = array_fill_keys($column, 0);
        foreach ($joinlist as $k => $v) {
            $userlist[$v['join_date']] = $v['nums'];
        }

        $dbTableList = Db::query("SHOW TABLE STATUS");
        $addonList = get_addon_list();
        $totalworkingaddon = 0;
        $totaladdon = count($addonList);
        foreach ($addonList as $index => $item) {
            if ($item['state']) {
                $totalworkingaddon += 1;
            }
        }
        $this->view->assign([
            'totaluser'         => User::count(),
            'totaladdon'        => $totaladdon,
            'totaladmin'        => Admin::count(),
            'totalcategory'     => \app\common\model\Category::count(),
            'todayusersignup'   => User::whereTime('jointime', 'today')->count(),
            'todayuserlogin'    => User::whereTime('logintime', 'today')->count(),
            'sevendau'          => User::whereTime('jointime|logintime|prevtime', '-7 days')->count(),
            'thirtydau'         => User::whereTime('jointime|logintime|prevtime', '-30 days')->count(),
            'threednu'          => User::whereTime('jointime', '-3 days')->count(),
            'sevendnu'          => User::whereTime('jointime', '-7 days')->count(),
            'dbtablenums'       => count($dbTableList),
            'dbsize'            => array_sum(array_map(function ($item) {
                return $item['Data_length'] + $item['Index_length'];
            }, $dbTableList)),
            'totalworkingaddon' => $totalworkingaddon,
            'attachmentnums'    => Attachment::count(),
            'attachmentsize'    => Attachment::sum('filesize'),
            'picturenums'       => Attachment::where('mimetype', 'like', 'image/%')->count(),
            'picturesize'       => Attachment::where('mimetype', 'like', 'image/%')->sum('filesize'),
        ]);

        $this->assignconfig('column', array_keys($userlist));

        $this->assignconfig('userdata', array_values($userlist));
        return $this->view->fetch();
    }
    public function index(){
        $input = input();
        $where2 = [];
        if (!empty($input['timeCreatedEnd'])) {
            $where2[] = ['time_created','<=',strtotime($input['timeCreatedEnd']) + 86399];
        }
        if (!empty($input['timeCreatedStart'])) {
            $where2[] = ['time_created','>=',strtotime($input['timeCreatedStart'])];
        }
        $startTime = strtotime(date('Y-m-d'));
        $endTime = strtotime(date('Y-m-d')) + 86399;
        $data = [
            'orderMoneyCount' => 0, //总流水
            'orderMoneyDayCount' => 0,
            'orderMoneyWinCount' => 0,//总盈亏
            'orderMoneyWinDayCount' => 0,
            'orderCount' => 0,//订单数量
            'orderDayCount' => 0,
            'orderMemberCount' => 0,//下单用户数量
            'orderMemberDayCount' => 0,

            'totaluser' => 0,//会员数量
            'today_add' => 0,
            'totaladdon' => 0,//充值金额
            'today_push' => 0,
            'Total category' => 0,//提现金额
            'today_withdrawal' => 0,
            'attachmentnums' => 0,//有效用户
        ];
        $where = [
            'ghost' => 0
        ];
        //总流水
        $data['orderMoneyCount'] = Db::table('product_orders')->where($where2)->where($where)->sum('money_order');
        $data['orderMoneyDayCount'] = Db::table('product_orders')->where($where)->whereBetween('time_created',"$startTime,$endTime")->sum('money_order');
        //盈亏
        $data['orderMoneyWinCount'] = $data['orderMoneyCount'] - Db::table('product_orders')->where($where2)->where(['ghost' => 0,'is_win' => 'win'])->sum('order_win');
        $data['orderMoneyWinDayCount'] = $data['orderMoneyDayCount'] - Db::table('product_orders')->where(['ghost' => 0,'is_win' => 'win'])->whereBetween('time_created',[$startTime,$endTime])->sum('order_win');
        //订单数量
        $data['orderCount'] =  Db::table('product_orders')->where($where2)->where($where)->count();
        $data['orderDayCount'] = Db::table('product_orders')->where($where)->whereBetween('time_created',"$startTime,$endTime")->count();
        //下单用户数量
        $data['orderMemberCount'] = Db::table('product_orders')->where($where)->where($where2)->group('member_id')->count();
        $data['orderMemberDayCount'] = Db::table('product_orders')->where($where)->whereBetween('time_created',"$startTime,$endTime")->group('member_id')->count();

        //会员数量
        $data['totaluser'] = Db::table('members')->where($where)->count();
        $data['attachmentnums'] = Db::table('members')->where($where)->where($where2)->where('money','>',0)->count();
        $data['today_add'] = Db::table('members')->where($where)->whereBetween('time_created',"$startTime,$endTime")->count();
        //充值金额
        $data['totaladdon'] = Db::table('member_recharges')->where($where)->where($where2)->where(['status' => 'success'])->where('source','<>','caijin')->sum('money');
        $data['today_push'] = Db::table('member_recharges')->where($where)->where(['status' => 'success'])->where('source','<>','caijin')->whereBetween('time_created',"$startTime,$endTime")->sum('money');
        //提现金额
        $data['Total category'] = Db::table('member_withdrawals')->where($where)->where($where2)->where(['status' => 'success'])->sum('money');
        $data['today_withdrawal'] = Db::table('member_withdrawals')->where($where)->where(['status' => 'success'])->whereBetween('time_created',"$startTime,$endTime")->sum('money');
        //
        $this->assignconfig('column', array_keys($data));

        $this->assignconfig('userdata', array_values($data));
        $this->view->assign($data);
        return $this->fetch();
    }
}
